package com.jit.BluetoothKey.utils;

import android.annotation.SuppressLint;
import android.util.Base64;

import com.clj.fastble.utils.HexUtil;

import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

/**
 * Created by Administrator on 2018/2/26.
 */

public class AES128 {
    private static final String UTF_8 = "UTF-8";


    @SuppressLint("TrulyRandom")
    public static String encrypt(String key, String src) {
        try {
            if (key == null || key.length() <= 0 || src == null || src.length() <= 0) return null;
            SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] byteContent = src.getBytes(UTF_8);
            cipher.init(Cipher.ENCRYPT_MODE, skey);// 初始化
            byte[] result = cipher.doFinal(byteContent);
            return HexUtil.formatHexString(result); // 加密
        } catch (Exception ext) {
            ext.printStackTrace();
        }
        return null;
    }


    private static String byte2Base64(byte[] encode) {
        try {
            return Base64.encodeToString(encode, Base64.NO_WRAP);

        } catch (Exception ext) {
            ext.printStackTrace();
        }

        return null;
    }
}